<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <title>金属按钮</title>
  <link rel="icon" href="https://img.kaikeba.com/kkb_portal_icon.ico">
  <style>
    body {
      margin: 0;
      overflow: hidden;
    }

    #canvas {
      background-color: antiquewhite;
    }
  </style>
</head>

<body>
  <canvas id="canvas"></canvas>
  <!-- 顶点着色器 -->
  <script id="vertexShader" type="x-shader/x-vertex">
    attribute vec4 a_Position;
    void main(){
      gl_Position=a_Position;
    }
  </script>
  <!-- 片元着色器 -->
  <script id="fragmentShader" type="x-shader/x-fragment">
    precision mediump float;
    uniform vec2 u_CanvasSize;
    vec2 center=u_CanvasSize/2.0;
    float diagLen=length(center);
    float pi2=radians(360.0);
    float omega=4.0;
    float a=0.5;

    //渐变
    float gradient(float ang){
      return a*sin(omega*ang)+0.5; ;
    }

    //水平拉丝
    float wire(vec2 v){
      vec2 a= vec2(0.0,1.0);
      float n= dot(v,a);
      return fract(tan(n)*10000.0);
    }

    //杂色
    float noise(vec2 v){
      vec2 a= vec2(0.1234,0.5678);
      float n= dot(v,a);
      return fract(tan(n)*10000.0);
    }

    //获取弧度
    float getAngle(vec2 v){
      float ang=atan(v.y,v.x);
      if(ang<0.0){
          ang+=pi2;
      }
      return ang;
    }

    void main(){
      vec2 p=gl_FragCoord.xy-center;
      //极径
      float len=length(p);
      //极角
      float ang=getAngle(p);
      
      float x=u_CanvasSize.x*ang/pi2;
      float y=(len/diagLen)*u_CanvasSize.y;

      //渐变
      float f1 = gradient(ang);
      f1=0.65*f1+0.5;

	    //拉丝
      float f2 = wire(vec2(int(x),int(y)));
      f2=clamp(f2,0.75,0.8);

	    //杂色
      float f3 = noise(gl_FragCoord.xy);
      f3*=0.07;

      //复合亮度
      float f=f1*f2+f3;

      //凹凸效果
      float ratio1=smoothstep(-1.0,1.0,sin(ang));
      float ratio2=1.0-ratio1;
      /*
      float r=150.0;
      float expand1=r+4.0;
      float expand2=expand1+12.0;
      float expand3=expand2+2.0;
      if(len>r&&len<expand1){
          f*=ratio1+0.3;
      }else if(len>expand1&&len<expand2){
          f*=ratio2+0.1;
      }else if(len>expand2&&len<expand3){
          f*=ratio2+0.3;
      }
      */
      float ls[3];
      ls[0]=f*(ratio1+0.3);
      ls[1]=f*(ratio2+0.1);
      ls[2]=f*(ratio2+0.3);

      //初始半径
      float r=150.0;
      //半径集合
      float rs[3];
      rs[0]=r+4.0;
      rs[1]=rs[0]+12.0;
      rs[2]=rs[1]+2.0;

      //基于len值，计算片元亮度
      for(int i=0;i<3;i++){
          if(len>=r&&len<rs[i]){
              f=ls[i];
              break;
          }
          r=rs[i];
      }

      gl_FragColor=vec4(
        vec3(f),
        1
      );
    }
  </script>
  <script type="module">
    import { initShaders } from "../jsm/Utils.js";
    import Poly from './jsm/Poly.js'

    const canvas = document.querySelector("#canvas");
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;

    // 获取着色器文本
    const vsSource = document.querySelector("#vertexShader").innerText;
    const fsSource = document.querySelector("#fragmentShader").innerText;

    //三维画笔
    const gl = canvas.getContext("webgl");

    //初始化着色器
    initShaders(gl, vsSource, fsSource);

    //声明颜色 rgba
    gl.clearColor(0, 0, 0, 1);

    const source = new Float32Array([
      -1, 1,
      -1, -1,
      1, 1,
      1, -1
    ]);

    const rect = new Poly({
      gl,
      source,
      type: 'TRIANGLE_STRIP',
      attributes: {
        a_Position: {
          size: 2,
          index: 0
        }
      },
      uniforms: {
        u_CanvasSize: {
          type: 'uniform2fv',
          value: [canvas.width, canvas.height]
        }
      }
    })

    gl.clear(gl.COLOR_BUFFER_BIT);
    rect.draw()

  </script>
</body>

</html>